package com.ruoyi.email.controller;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.email.domain.TmdEmailWrite;
import com.ruoyi.email.service.ITmdEmailWriteService;
import com.ruoyi.email.vo.TmdEmailRubbishAndWrite;
import com.ruoyi.email.vo.TmdEmailWriteAndSysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.email.domain.TmdEmailRubbish;
import com.ruoyi.email.service.ITmdEmailRubbishService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 垃圾箱Controller
 * 
 * @author ruoyi
 * @date 2024-04-20
 */
@Controller
@RequestMapping("/email/manage")
public class TmdEmailRubbishController extends BaseController
{
    private String prefix = "email/manage";

    @Autowired
    private ITmdEmailRubbishService tmdEmailRubbishService;
    @Autowired
    private ITmdEmailWriteService tmdEmailWriteService;

    @RequiresPermissions("email:manage:view")
    @GetMapping()
    public String manage()
    {
        return prefix + "/manage";
    }

    /**
     * 查询垃圾箱列表
     */
    @RequiresPermissions("email:manage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TmdEmailWrite tmdEmailWrite)
    {
        startPage();
        List<TmdEmailRubbishAndWrite> list = tmdEmailRubbishService.selectTmdEmailRubbishList(tmdEmailWrite);
        return getDataTable(list);
    }


    /**
     * 新增垃圾箱
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存垃圾箱
     */
    @RequiresPermissions("email:manage:add")
    @Log(title = "垃圾箱", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TmdEmailRubbish tmdEmailRubbish)
    {
        return toAjax(tmdEmailRubbishService.insertTmdEmailRubbish(tmdEmailRubbish));
    }

    /**
     * 修改垃圾箱
     */
    @RequiresPermissions("email:manage:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") int id, ModelMap mmap)
    {
        TmdEmailRubbishAndWrite tmdEmailRubbishAndWrite = tmdEmailRubbishService.selectTmdEmailRubbishById(id);
        mmap.put("tmdEmailRubbish", tmdEmailRubbishAndWrite);
        return prefix + "/edit";
    }

    /**
     * 修改保存垃圾箱
     */
    @RequiresPermissions("email:manage:edit")
    @Log(title = "垃圾箱", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TmdEmailRubbish tmdEmailRubbish)
    {
        return toAjax(tmdEmailRubbishService.updateTmdEmailRubbish(tmdEmailRubbish));
    }

    /**
     * 删除垃圾箱
     */
    @RequiresPermissions("email:manage:remove")
    @Log(title = "垃圾箱", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tmdEmailRubbishService.deleteTmdEmailRubbishByIds(ids));
    }
    @RequiresPermissions("email:manage:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap mmap)
    {
        TmdEmailRubbishAndWrite tmdEmailRubbishAndWrite = tmdEmailRubbishService.selectTmdEmailRubbishById(id);
        mmap.put("tmdEmailRubbishAndWrite", tmdEmailRubbishAndWrite);
        return prefix + "/detail";
    }

    @RequiresPermissions("email:manage:reduction")
    @Log(title = "垃圾箱", businessType = BusinessType.DELETE)
    @PostMapping( "/reduction")
    @ResponseBody
    public AjaxResult reduction(String ids)
    {
        String[] strings = Convert.toStrArray(ids);
        for (String eve:strings) {
            int i = Integer.parseInt(eve);
            TmdEmailRubbishAndWrite tmdEmailRubbishAndWrite = tmdEmailRubbishService.selectTmdEmailRubbishById(i);
            if(tmdEmailRubbishAndWrite.getPostStaus()!=null && tmdEmailRubbishAndWrite.getPostStaus()==1){
                TmdEmailWrite tmdEmailWrite = new TmdEmailWrite();
                tmdEmailWrite.setId(tmdEmailRubbishAndWrite.getEmailId());
                tmdEmailWrite.setPostStaus(1);
                tmdEmailWriteService.updateTmdEmailWrite(tmdEmailWrite);
            }else if(tmdEmailRubbishAndWrite.getReceiveStatus()!=null && tmdEmailRubbishAndWrite.getReceiveStatus()==1){
                TmdEmailWrite tmdEmailWrite = new TmdEmailWrite();
                tmdEmailWrite.setId(tmdEmailRubbishAndWrite.getEmailId());
                tmdEmailWrite.setReceiveStatus(1);
                tmdEmailWriteService.updateTmdEmailWrite(tmdEmailWrite);
            }else if(tmdEmailRubbishAndWrite.getDraftStatus()!=null && tmdEmailRubbishAndWrite.getDraftStatus()==1){
                TmdEmailWrite tmdEmailWrite = new TmdEmailWrite();
                tmdEmailWrite.setId(tmdEmailRubbishAndWrite.getEmailId());
                tmdEmailWrite.setDraftStatus(1);
                tmdEmailWriteService.updateTmdEmailWrite(tmdEmailWrite);
            }
        }
        return toAjax(tmdEmailRubbishService.deleteTmdEmailRubbishByIds(ids));
    }
}
